home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: judgemi@ix.netcom.com (Michael Judge )
- Newsgroups: comp.lang.c++
- Subject: Re: error checking for int
- Date: 3 Feb 1996 18:30:14 GMT
- Organization: Netcom
- Message-ID: <4f09jm$ob3@reader2.ix.netcom.com>
- References: <Pine.SUN.3.91.960203120230.12797A-100000@chip.bgsu.edu>
- NNTP-Posting-Host: bos-ma10-09.ix.netcom.com
- X-NETCOM-Date: Sat Feb 03 10:30:14 AM PST 1996
-
- In <Pine.SUN.3.91.960203120230.12797A-100000@chip.bgsu.edu> Andrew
- Turner <turner@chip.bgsu.edu> writes:
- >
- >
- > I have an array of ints declared
- >
- > int something[20];
- >
- >and when I pass it a char it goes into an endless loop, ex.
- >
- > cout>>"\nEnter a number from 1-20: ";
- > cin<<something[0];
- >
-
- First problem is that you have the ">>" and "<<" backward in a few
- places, I assume they are typos.
- I tried this in Visual C++ in a quickwin application and it produces a
- 0 for something[0]. If that is not the case then you have a problem in
- your iostream library.
-
- >if the person enters a char then the program goes bonkers. One
- >modification that I have tried is
- >
- > cout>>"\nEnter a number from 1-20: ";
- > cin<<temp; //where temp is declared as an int.
- > while ((temp<1) || (temp >20))
- > {
- > temp=0;
- > cout>>"\nEnter a valid number from 1-20: ";
- > cin>>temp;
- > }
- > something[0]=temp;
- >
- >but all this seems to do is go into a loop that prints
- >
- > Enter a valid number from 1-20:
- >
- >to the screen a million times or until I break out of it.
-
-
- solve this with a sync_with_stdio. it is a method of ios.
-
- int temp;
- int something[20];
- cout<<"\nEnter a number from 1-20: ";
- cin>>temp; //where temp is declared as an int.
- while ((temp<1) || (temp >20))
- {
- //next line is key
- cin.sync_with_stdio();
- temp=0;
- cout<<"\nEnter a valid number from 1-20: ";
- cin>>temp;
- }
- something[0]=temp;
-
- MJ
-
-